package com.allwees.bs.module.fms.service.impl;

import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.fms.dao.RetailerSettleStatementDao;
import com.allwees.bs.module.fms.entity.RetailerSettleStatement;
import com.allwees.bs.module.fms.req.DoSettleReq;
import com.allwees.bs.module.fms.service.MerchantAccountLogService;
import com.allwees.bs.module.fms.service.MerchantAccountService;
import com.allwees.bs.module.fms.service.RetailerSettleStatementService;
import com.allwees.bs.module.oms.account.runner.AccountTransitUtils;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
import com.allwees.core.common.constant.EStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 *
 * @author yangylsky
 * @since 2019-12-12
 *
 */
@Service
public class RetailerSettleStatementServiceImpl extends BoServiceImpl<RetailerSettleStatementDao, RetailerSettleStatement>
		implements RetailerSettleStatementService {
	@Autowired
	private GeneralOmsOrderItemService _generalOmsOrderItemService;

	@Autowired
	private MerchantAccountService merchantAccountService;

	@Autowired
	private MerchantAccountLogService merchantAccountLogService;

	@Autowired
	private AccountTransitUtils accountTransitUtils;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void doSettle(OmsOrderItem omsOrderItem,DoSettleReq req, boolean exceedSettleDate ) {
		if(omsOrderItem.getEStatus().getValue() == EStatus.OMS_REFUNDING.getValue() || omsOrderItem.getEStatus().getValue() == EStatus.OMS_REFUND.getValue() ) {
			return;
		}
		_generalOmsOrderItemService.doSettled(omsOrderItem,req.getBranchUuid(),exceedSettleDate);
		accountTransitUtils.accountTransit(omsOrderItem,"doSettle");

		/*RetailerSettleStatement settleStatement = _createSettleStatement(req.getBranchUuid(), orderItem);
		MerchantAccount account = merchantAccountService.findDefaultMerchantAccount(EBranchType.RETAILER, settleStatement.getRetailerUuid());
		Assert.notNull(account, "商户[" + settleStatement.getRetailerUuid() + "]结算账户为空");
		account.doSettle(settleStatement);*/
		/*merchantAccountService.updateByUuid(account);*/

		//_logSettle(account, settleStatement);
		//return settleStatement;
	}

	/*@Override
	@Transactional(rollbackFor = Exception.class)
	public void doUnSettle(OmsOrderItem omsOrderItem) {
		MerchantAccount account = merchantAccountService.findDefaultMerchantAccount(EBranchType.RETAILER, omsOrderItem.getRetailerUuid());
		Assert.notNull(account, "商户[" + omsOrderItem.getRetailerUuid() + "]结算账户为空");
		account.calculateUnSettle(omsOrderItem);
		merchantAccountService.updateByUuid(account);

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void doWaitingShipAmt(  OrderItem orderItem ){
		MerchantAccount account = merchantAccountService.findDefaultMerchantAccount(EBranchType.RETAILER, orderItem.getRetailerUuid());
		//Assert.notNull(account, "商户[" +  orderItem.getRetailerUuid() + "]结算账户为空");
		account.calculateWaitingShipAmt(orderItem);
		merchantAccountService.updateByUuid(account);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void doRefundedAmt(OrderItem orderItem){
		MerchantAccount account = merchantAccountService.findDefaultMerchantAccount(EBranchType.RETAILER, orderItem.getRetailerUuid());
		//Assert.notNull(account, "商户[" +  orderItem.getRetailerUuid() + "]结算账户为空");
		account.calculateRefundedAmt(orderItem);
		merchantAccountService.updateByUuid(account);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void doInShippingAmt( OmsOrderItem omsOrderItem) {
		MerchantAccount account = merchantAccountService.findDefaultMerchantAccount(EBranchType.RETAILER, omsOrderItem.getRetailerUuid());
		//Assert.notNull(account, "商户[" +  omsOrderItem.getRetailerUuid() + "]结算账户为空");
		account.calculateInShippingAmt(omsOrderItem);
		merchantAccountService.updateByUuid(account);

	}*/

	/*private RetailerSettleStatement _createSettleStatement(String retailerUuid, OmsOrderItem omsOrderItem) {
		RetailerSettleStatement settleStatement = new RetailerSettleStatement();
		settleStatement.setRetailerUuid(retailerUuid);
		settleStatement.setOrderNo(omsOrderItem.getOrderNo());
		settleStatement.setOrderItemUuid(omsOrderItem.getUuid());
		settleStatement.setOrderUuid(omsOrderItem.getOrderUuid());
		settleStatement.setNo(FmsNoUtil.genSettleStatementNo(EBranchType.RETAILER, retailerUuid));
		settleStatement.setCurrency(CurrencyCode.USD.name());
		BigDecimal amountTotal = BigDecimal.ZERO;

		BigDecimal skuAmount = MoneyUtil.mul(omsOrderItem.getPriceAdjust(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		BigDecimal skuShippingAmount = MoneyUtil.mul(omsOrderItem.getShippingPriceAdjust(), BigDecimal.valueOf(omsOrderItem.getQuantity()));
		amountTotal = MoneyUtil.add(amountTotal, skuAmount,skuShippingAmount);

		settleStatement.setAmount(amountTotal);
		save(settleStatement);
		return settleStatement;
	}

	private void _logSettle(MerchantAccount account, RetailerSettleStatement settleStatement) {
		MerchantAccountLog accountLog = new MerchantAccountLog();
		accountLog.setBranchUuid(account.getBranchUuid())
				.setBalanceType(EBalanceType.INCOME)
				.setBsType(EAccountLogBsType.ORDER_SETTLED)
				.setNo(FmsNoUtil.genAccountLogNo(account.getBranchUuid(), EBalanceType.INCOME, EAccountLogBsType.ORDER_SETTLED))
				.setAccountUuid(account.getUuid())
				.setAccountNo(account.getAccountNo())
				.setTakeAmount(settleStatement.getAmount())
				.setCurrency(settleStatement.getCurrency())
				.setFee(BigDecimal.ZERO)
				.setBalance(account.getBalance())
				.setBalanceCurrency(account.getCurrency());
		merchantAccountLogService.create(accountLog);
	}*/
}
